package cn.gailvlun.gll.widget

import android.content.Context
import android.graphics.Color
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.util.Log
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import cn.gailvlun.gll.R

class CountDownView : ViewGroup {
    constructor(context: Context?) : this(context, null)
    constructor(context: Context?, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        View.inflate(context, R.layout.view_count_down, this)

        ivClock = findViewById(R.id.iv_clock)
        tvCountDown = findViewById(R.id.tv_count_down)

        countDownDrawable = context?.resources?.getDrawable(R.drawable.ic_countdown)
        countDownRushDrawable = context?.resources?.getDrawable(R.drawable.ic_countdown_rush)

        countDownColor = context?.resources?.getColor(R.color.apricot)
        countDownRushColor = Color.parseColor("#ff93ac")

        ivClock?.setImageDrawable(countDownDrawable)
    }

    private var ivClock: ImageView? = null
    private var tvCountDown: TextView? = null

    private var countDownDrawable: Drawable? = null
    private var countDownRushDrawable: Drawable? = null

    private var countDownColor: Int? = null
    private var countDownRushColor: Int? = null

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        var maxWidth = 0
        var maxHeight = 0
        for (index in 0 until childCount) {
            val child = getChildAt(index)
            measureChild(child, widthMeasureSpec, heightMeasureSpec)
            maxWidth = Math.max(child.measuredWidth, maxWidth)
            maxHeight = Math.max(child.measuredHeight, maxHeight)
        }

        setMeasuredDimension(maxWidth, maxHeight)
    }

    override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
        for (index in 0 until childCount) {
            val view = getChildAt(index)
            if (view == ivClock) {
                view.layout(0, 0, r - l, b - t)
            } else if (view == tvCountDown) {
                
                val width = view.measuredWidth.apply {
                    Log.d("test", "width $this")
                }
                val height = view.measuredHeight.apply {
                    Log.d("test", "width $this")
                }

                view.layout((r - l) / 2 - width / 2,
                        (b - t) / 2 - height / 2,
                        (r - l) / 2 + width / 2,
                        (b - t) / 2 + height / 2)
            }
        }
    }

    var isRush: Boolean = false

    fun toggle() {
        if (!isRush) {
            ivClock?.setImageDrawable(countDownRushDrawable)
            tvCountDown?.setTextColor(countDownRushColor!!)
        } else {
            ivClock?.setImageDrawable(countDownDrawable)
            tvCountDown?.setTextColor(countDownColor!!)
        }
        isRush = !isRush
    }

    fun setText(seconds: Int) {
        tvCountDown?.text = "${seconds}s"
    }

}